
"""
    # http://www.wangpan1024.com 对这个网站下手
    免费影视资源网站，就连新年贺岁片都有，不过看样子就是最新的影视有
    我搜了一下  《战狼》 都找不到，也不知道这个网站还能干吗

    不过能看最新的影院电影也是不错的了，准备做个自己的网站，看来需要这个数据
    看起来这个网站也是挺过分了，不过我喜欢，我也来分一杯羹 嘻嘻

"""


import requests
import re
import os
import logging
from lxml import etree
import json

# 设置基本配置信息
LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
DATE_FORMAT = "%m/%d/%Y %H:%M:%S %p"

logging.basicConfig(filename='m3u8.log', format=LOG_FORMAT, datefmt=DATE_FORMAT,level = logging.INFO)


def guolv(txt):
    txt = txt.replace('：','')
    txt = txt.replace('/','')
    txt = txt.replace(',','..')
    return txt


# 获取网页内容
def get_html_obj(url):
    '''如果获取网页失败返回空,如果成功返回网页对象r'''
    try:
        r = requests.get(url,timeout=100)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r
    except:
        logging.error('html_obj timeout error:URL:{}'.format(url))
        return ''

# 获取页面的电影总页数


def get_m3u8_list_count(html_text):
    try:
        m3u8_list_count = re.findall(
            '<a class="page_link" href="/index.php/vod/search/page/(.*)/wd/.*" title="尾页">尾页</a>', html_text)
        return m3u8_list_count[0]
    except:
        logging.error('get_m3u8_list_count error not found')
        return 0

# 解析页面内容元素

def pars_m3u8_listinfo(html_text):
    Xm3u8_URL = '//*[@id="list-focus"]/ul/li/div/h2/a/@href'
    Xm3u8_Name = '//*[@id="list-focus"]/ul/li/div/h2/a/text()'
    Xm3u8_Status = '//*[@id="list-focus"]/ul/li/div/dl[2]/dd/span/text()'
    Xm3u8_Starring = '//*[@id="list-focus"]/ul/li/div/dl[1]/dd/text()'
    Xm3u8_Director = '//*[@id="list-focus"]/ul/li/div/dl[3]/dd/text()'
    Xm3u8_Type = '//*[@id="list-focus"]/ul/li/div/dl[4]/dd/text()'
    Xm3u8_Region = '//*[@id="list-focus"]/ul/li/div/dl[5]/dd/span/text()'
    Xm3u8_Time = '//*[@id="list-focus"]/ul/li/div/dl[6]/dd/span/text()'
    Xm3u8_Year = '//*[@id="list-focus"]/ul/li/div/dl[7]/dd/span/text()'
    Xm3u8_JuQing = '//*[@id="list-focus"]/ul/li/div/dl[8]/dd/text()'
    Xm3u8_Images = '//*[@id="list-focus"]/ul/li/a/img/@src'

    html = etree.HTML(html_text)

    m3u8_URL = html.xpath(Xm3u8_URL)  # 跳转链接
    m3u8_Name = html.xpath(Xm3u8_Name)  # 名称
    m3u8_Status = html.xpath(Xm3u8_Status)  # 状态
    m3u8_Starring = html.xpath(Xm3u8_Starring)  # 主演
    m3u8_Director = html.xpath(Xm3u8_Director)  # 导演
    m3u8_Type = html.xpath(Xm3u8_Type)  # 类型
    m3u8_Region = html.xpath(Xm3u8_Region)  # 地区
    m3u8_Time = html.xpath(Xm3u8_Time)  # 时间
    m3u8_Year = html.xpath(Xm3u8_Year)  # 年份
    m3u8_JuQing = html.xpath(Xm3u8_JuQing)  # 剧情
    m3u8_Images = html.xpath(Xm3u8_Images)  # 图片

    m3u8_URL = ['http://www.wangpan1024.com{}'.format(i) for i in m3u8_URL]
    return zip(m3u8_URL, m3u8_Name, m3u8_Status, m3u8_Starring, m3u8_Director, m3u8_Type, m3u8_Region, m3u8_Time, m3u8_Year, m3u8_JuQing, m3u8_Images)


# 获取 m3u8 视频原地址
def get_m3u8_URL(m3u8_listinfo_URL):
    r = get_html_obj(m3u8_listinfo_URL)
    if r != '':
        html_text = r.text

        Xpath = '//*[@class="video_list fn-clear"]/a/@href'
        Xpath1 = '//*[@class="video_list fn-clear"]/a/text()'
        html = etree.HTML(html_text)
        m3u8_URLS = html.xpath(Xpath)
        m3u8_URLS_status = html.xpath(Xpath1)

        URLS = list()
        m3u8_URLS = [
            'http://www.wangpan1024.com{}'.format(i) for i in m3u8_URLS]
        for i in m3u8_URLS:
            r = get_html_obj(i)
            if r != '':
                html_text = r.text
                m3u8_play_URL = re.findall(
                    '<script>var player_data=(.*)}</script>', html_text)[0] + '}'
                m3u8_play_URL = json.loads(m3u8_play_URL)
                URLS.append(m3u8_play_URL['url'])
            else:
                URLS.append('丢失')
                logging.error('m3u8_URL not found')
        print(URLS)
        return zip(URLS, m3u8_URLS_status)
    else:
        logging.error('get_m3u8_URL:' + m3u8_listinfo_URL)
        return None


def mkdir(dirName):
    if(not os.path.exists(dirName)):
        os.makedirs(dirName)
        print(dirName + '   创建成功')
    else:
        print(dirName + '   目录已存在')
    return dirName


def save_m3u8_play_info(name,m3u8_Name, m3u8_Status, m3u8_Starring, m3u8_Director, m3u8_Type, m3u8_Region, m3u8_Time, m3u8_Year, m3u8_JuQing, m3u8_Images, URLS):
    tmpName = './{}/{}.csv'.format(name, m3u8_Name)
    if(os.path.exists(tmpName)):
        os.remove(tmpName)
    with open(tmpName, 'w', encoding='utf-8') as f:
        #f.write('电影名,状态,主演,导演,类型,地区,时间,年份,剧情,图片,视频源地址\n')
        f.write(m3u8_Name+'\n'+m3u8_Status+'\n'+m3u8_Starring+'\n'+m3u8_Director+'\n'+m3u8_Type+'\n'+m3u8_Region +
                '\n'+m3u8_Time+'\n'+m3u8_Year+'\n'+m3u8_JuQing+'\n'+m3u8_Images+'\n资源：\n'+URLS+'\n')
        f.close()

def main(html_text):
    try:
        a = pars_m3u8_listinfo(html_text)
        for m3u8_URL, m3u8_Name, m3u8_Status, m3u8_Starring, m3u8_Director, m3u8_Type, m3u8_Region, m3u8_Time, m3u8_Year, m3u8_JuQing, m3u8_Images in a:
            print(m3u8_URL, m3u8_Name, m3u8_Status, m3u8_Starring, m3u8_Director,
                    m3u8_Type, m3u8_Region, m3u8_Time, m3u8_Year, m3u8_JuQing, m3u8_Images)
            b = get_m3u8_URL(m3u8_URL)
            tmp_URLS = ''
            for URLS, m3u8_URLS_status in b:
                print(URLS, m3u8_URLS_status)
                tmp_URLS = tmp_URLS + URLS + '\t|' + m3u8_URLS_status + '\n'

            save_m3u8_play_info(name,guolv(m3u8_Name), m3u8_Status, m3u8_Starring, guolv(m3u8_Director), m3u8_Type, m3u8_Region,
                                m3u8_Time, m3u8_Year, m3u8_JuQing, m3u8_Images, tmp_URLS)
    except:
        logging.error('object is not found')

if __name__ == "__main__":
    # URL http://www.wangpan1024.com/index.php/vod/search.html?wd=%E6%88%98%E7%8B%BC
    
    print('输入你想看的电影，但不保证一定存在：')
    name = input()
    url = 'http://www.wangpan1024.com/index.php/vod/search.html?wd={}'.format(
        name)

    mkdir(name)

    r = get_html_obj(url)
    if r != '':
        html_text = r.text
        m3u8_list_count = get_m3u8_list_count(html_text)
        print('共有{}页信息数据'.format(m3u8_list_count))
        if m3u8_list_count == 0:
            logging.info('videos not found')
            print('没有找到任何电影')
            exit()
        print('当前是第 1页的数据》》》》》》》》》》》》》》》》》》》》》》')
        main(html_text)
        print('当前是第 1页的结束《《《《《《《《《《《《《《《《《《《《《《')

        for i in range(2,int(m3u8_list_count)+1):
            url = 'http://www.wangpan1024.com/index.php/vod/search/page/{}/wd/{}.html'.format(str(i),name)
            print('当前是第 {}页的数据》》》》》》》》》》》》》》》》》》》》》》'.format(str(i)))
            r = get_html_obj(url)
            if r != '':
                html_text = r.text
                main(html_text)
                print('当前是第 {}页的结束《《《《《《《《《《《《《《《《《《《《《《'.format(str(i)))
            else:
                logging.error('get html obj error')
    else:
        logging.error('get html obj error')

